var fun={
    init:function () {
        $('#in_errorMsg').text('');
        fun.timeInit();
        fun.sumFun();
        fun.initSelect();
    },
    timeInit:function () {

        $('#subscribeTime').val(moment().subtract('days', 0).format('YYYY-MM-DD HH:00:00'));
        $("#subscribeTime").datetimepicker({
            startDate: moment().subtract('second', 0).format('yyyy-mm-dd hh:ii:00'),
            language:  "zh-CN",
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            // startView: 2,
            // minView: 2,
            // forceParse: 0,
            format: 'yyyy-mm-dd hh:ii:00',
            minuteStep:30,//分钟间隔10分钟
            forceParse: false,
            // clearBtn: true, //添加清除按钮，可选值：true/false
        });

    },
    sumFun:function () {
        var num_add = document.getElementById("num_add");
        var num_sub = document.getElementById("num_sub");
        var input_num = document.getElementById("subscribePeopNum");

        num_add.onclick = function() {
            if(subscribePeopNum.value <= 6) {
                subscribePeopNum.value = parseInt(subscribePeopNum.value) + 1;
            }

        }

        num_sub.onclick = function() {

            if(subscribePeopNum.value <= 1) {
                subscribePeopNum.value = 1;
            } else {

                subscribePeopNum.value = parseInt(subscribePeopNum.value) - 1;
            }

        }
    },
    submitFun:function () {
        $("#in_userSubmit").attr("disabled","disabled");
        var subscribeTime= $("#subscribeTime").val();//时间
        var fromCityNo= $("#from_city_no").find("option:selected").val();//出发点
        var fromCityName= $("#from_city_no").find("option:selected").text();//出发点
        var toCityNo= $("#to_city_no").find("option:selected").val();//终点
        var toCityName= $("#to_city_no").find("option:selected").text();//终点
        var carType =$('input:radio[name="carType"]:checked').val(); //车辆类型
        var subscribeType =$('input:radio[name="subscribeType"]:checked').val(); //预约类型
        var phoneNo = $("#phoneNo").val().trim();//电话
        var remarks = $("#remarks").val();//备注
        var openId = $("#openId").val();
        var subscribePeopNum = $("#subscribePeopNum").val();

        if(openId == null || openId=='' || openId== undefined){
            $("#in_userSubmit").removeAttr("disabled");
            $('#in_errorMsg').text("错误提示:获取用户openId失败！");
            return false;
        }

        if(fromCityName == toCityName){
            $('#in_errorMsg').text("错误提示:出发地和目的地不能相同！");
            $("#in_userSubmit").removeAttr("disabled");
            return false;
        }
        if(subscribeTime == null || subscribeTime == '' ||subscribeTime == undefined){
            $("#in_userSubmit").removeAttr("disabled");
            $('#in_errorMsg').text("错误提示:请选择出发时间！");
            return false;
        }
        if(phoneNo==null || phoneNo=='' ||phoneNo == undefined){
            $('#in_errorMsg').text("错误提示:请填写手机号码！");
            $("#in_userSubmit").removeAttr("disabled");
            return false;
        }
        if(phoneNo!=null && phoneNo!='' && !(/^1(3|4|5|7|8)\d{9}$/.test(phoneNo))){
            $('#in_errorMsg').text("错误提示:请填写正确的手机号码！");
            $("#in_userSubmit").removeAttr("disabled");
            return false;
        }else{
            $('#in_errorMsg').text("");
            var req={
                subscribeTime:subscribeTime,
                fromCityNo:fromCityNo,
                fromCityName:fromCityName,
                toCityNo:toCityNo,
                toCityName:toCityName,
                phoneNo:phoneNo,
                carType:carType,
                subscribeType:subscribeType,
                remarks:remarks,
                subscribeState:1,
                openId:openId,
                appId:sysComm.appId,
                subscribePeopNum:subscribePeopNum
            };
            var opts = {
                method:'post',
                url: dataUrl.util.saveTCarSubscribe(),
                data: JSON.stringify(req),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function(res) {
                    if(res.code =='8888'){
                        $("#in_userSubmit").removeAttr("disabled");
                        //alert(res);
                        alert('提交成功');
                        window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+sysComm.appId+'&redirect_uri='+wxcarbaseUrl+'mysubscribe.html&response_type=code&scope=snsapi_userinfo&state=azz#wechat_redirect';
                    }else{
                        $('#in_errorMsg').text("错误提示:预约失败,请重新预约");
                        $("#in_userSubmit").removeAttr("disabled");
                        return false;
                    }

                }
            };
            sysAjax(opts);




        }
    },
    /**
     *
     */
    initSelect:function() {
        $("#in_userSubmit").removeAttr("disabled");
        //1.加载出发城市信息
        commonObj.queryCarCityByType(1,function(data){
            var html ="";
            $.each(data,function(index,item){
                html+="<option value='"+item.id+"'>"+item.cityName+"</option>";
            });
            $("#from_city_no").empty();
            $("#from_city_no").html(html);
        });
        //2.加载目的城市信息
        commonObj.queryCarCityByType(2,function(data){
            var html ="";
            $.each(data,function(index,item){
                html+="<option value='"+item.id+"'>"+item.cityName+"</option>";
            });
            $("#to_city_no").empty();
            $("#to_city_no").html(html);
        });
        //获取到的code
        var code = getUrlParam("code");
        //获取openId
        var openId = commonObj.getOpenId(code);
        $("#openId").val("");
        $("#openId").val(openId);
        console.debug("code="+code);


    }

}
fun.init();

/*$("#subscribeTime").on("focus", function() {
    $(this).trigger("blur");
});
$("#subscribeTime").click(function(){
    var _this = this;
    var val = $(_this).data("datetime");
    var picker = new DatetimePicker(
        {
            onSelectCallback:function(picker){
                var value = picker.value;
                $(_this).val(value.join("-"));
                $(_this).data("datetime",value);
                picker.close();
            },
            value:val,
            format:"yyyy年mm月dd日"
        }
    );

    picker.open();
});*/

$(document).on('click','#in_userSubmit',function() {
    fun.submitFun();
});